﻿Option Explicit On

Imports System.Text
Imports CRM.BusinessLayer
Partial Public Class frmAdminEditEvent
    Private _OldMessage As String
    Private _OldDescription As String
    Private Sub frmAdminEditEvent_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
    End Sub

    Private Sub txtEventId_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles txtEventId.LostFocus
        Dim myEvent As TasksEvents.EventInfo
        Dim Msg As New StringBuilder

        If Me.txtEventId.Text.Trim = "" Then Exit Sub

        If Not IsNumeric(Me.txtEventId.Text) Then
            MsgBox("Not a valid Event ID", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If

        myEvent = (New TasksEvents).GetEvent(Me.txtEventId.Text)

        If myEvent.EventId = 0 Then
            MsgBox("Event not found, please check the ID and try again", MsgBoxStyle.Exclamation, "Error")
            Me.txtEventText.Text = ""
            Me.txtEventType.Text = ""

            Me.btnDelete.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            'Me.BtnMove.Enabled = False
            'Me.FrmMoveTo.Enabled = False
            'Me.TxtMoveNumber.Enabled = False
            Exit Sub
        End If

        Me.txtEventText.Text = myEvent.Message
        Me.txtEventType.Text = myEvent.Description

        Me._OldMessage = myEvent.Message
        Me._OldDescription = myEvent.Description

        'Show consultant Details

        With Msg


            Select Case myEvent.CategoryId

                Case 1
                    .Append("Entered by " & myEvent.EnteredBy & " on " & myEvent.DateEntered)

                Case 2
                    .Append("Scheduled Task" & vbCrLf)
                    .Append("Entered by " & myEvent.EnteredBy & " on " & myEvent.DateEntered & vbCrLf)
                    .Append("Scheduled for " & myEvent.ScheduledFor & " on " & myEvent.DateScheduled)


                Case 3
                    .Append("Completed Task" & vbCrLf)
                    .Append("Entered by " & myEvent.EnteredBy & " on " & myEvent.DateEntered & vbCrLf)
                    .Append("Scheduled for " & myEvent.ScheduledFor & " on " & myEvent.DateScheduled & vbCrLf)
                    .Append("Completed by " & myEvent.CompletedBy & " on " & myEvent.DateCompleted)

            End Select

        End With

        Me.lblCon.Content = Msg.ToString

        'Show Event Details
        Msg = New StringBuilder

        Select Case myEvent.LinkType

            Case 1
                Msg.Append("Linked to Case number " & myEvent.LinkId & vbCrLf)

                Dim myCase As Cases.CaseInfo = Cases.GetCaseInfo(myEvent.LinkId)

                If myCase.CaseId = 0 Then
                    Msg.Append("Link not found" & vbCrLf & "Please speak to IT")
                Else
                    Msg.Append(myCase.ProductName & vbCrLf & myCase.CandidateName & vbCrLf & myCase.CompanyName)
                End If


            Case 2
                Msg.Append("Linked to Person number " & myEvent.LinkId & vbCrLf)

                Dim myPerson As People.PersonInfo = People.GetPerson(myEvent.LinkId)


                If myPerson.PersonId = 0 Then
                    Msg.Append("Link not found" & vbCrLf & "Please speak to IT")
                Else
                    Msg.Append(myPerson.PersonId & vbCrLf & myPerson.CompanyId)
                End If


            Case 3
                Msg.Append("Linked to Company number " & myEvent.LinkId & vbCrLf)

                Dim myCompany As Company.CompanyInfo = Company.GetCompany(myEvent.LinkId)

                If myCompany.CompanyID = 0 Then
                    Msg.Append("Link not found" & vbCrLf & "Please speak to IT")
                Else
                    Msg.Append(myCompany.CompanyName)
                End If


            Case 4
                Msg.Append("User Task" & vbCrLf)


            Case Else


        End Select

        Me.lblEvent.Content = Msg.ToString

        Me.btnDelete.IsEnabled = True
        Me.btnEdit.IsEnabled = True
        Me.btnMoveTo.IsEnabled = True
        Me.grpMoveTo.isEnabled = True
        Me.txtMoveTo.IsEnabled = True

    End Sub


    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        If Me.btnClose.Content = "Close" Then
            Me.Close()
        Else
            Me.btnClose.Content = "Close"
            Me.btnDelete.IsEnabled = True
            Me.btnEdit.Content = "Edit"
            Me.btnMoveTo.IsEnabled = True
            Me.grpMoveTo.IsEnabled = True
            Me.txtMoveTo.IsEnabled = True

            Me.txtEventId.IsEnabled = True
            Me.txtEventText.IsReadOnly = True
            Me.txtEventType.IsReadOnly = True


        End If
    End Sub


    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click

        If MsgBox("Are you sure you want to delete this event?", vbYesNo, "Confirm delete") <> MsgBoxResult.Yes Then Exit Sub

        Dim Msg As New StringBuilder

        With Msg
            .Append("Event number " & Me.txtEventId.Text & " deleted" & vbCrLf & vbCrLf)
            .Append("Event Details" & vbCrLf & Me.lblEvent.Content & vbCrLf & vbCrLf)
            .Append("Con Details" & vbCrLf & Me.lblCon.Content & vbCrLf & vbCrLf)
            .Append(Me.txtEventType.Text & vbCrLf & vbCrLf & Me.txtEventText.Text)
        End With

        If (New TasksEvents).DeleteEvent(Me.txtEventId.Text, Msg.ToString) Then
            Me.txtEventId.Focus()

            Me.txtEventId.Text = ""
            Me.txtEventText.Text = ""
            Me.txtEventType.Text = ""
            Me.lblCon.Content = ""
            Me.lblEvent.Content = ""
            Me.chkNotify.IsEnabled = False
            Me.btnDelete.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            Me.btnMoveTo.IsEnabled = False
            Me.grpMoveTo.IsEnabled = False
            Me.txtMoveTo.IsEnabled = False

        End If

    End Sub


    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click
        If Me.btnEdit.Content = "Edit" Then
            Me.btnClose.Content = "Cancel"
            Me.btnDelete.IsEnabled = False
            Me.btnEdit.Content = "Save"
            Me.btnMoveTo.IsEnabled = False
            Me.grpMoveTo.IsEnabled = False
            Me.txtMoveTo.IsEnabled = False

            Me.txtEventText.IsReadOnly = False
            Me.txtEventType.IsReadOnly = False

            'If Security.CSec(53) = 1 Then
            '    Me.Chk_AddNote.Enabled = True
            'End If

        Else
            If Me.txtEventType.Text.Trim = "" Then
                MsgBox("Event type cannot be blank", MsgBoxStyle.Exclamation, "Error")
                Me.txtEventType.Focus()
                Exit Sub
            End If

            If Me.txtEventText.Text.Trim = "" Then
                MsgBox("Event message cannot be blank", MsgBoxStyle.Exclamation, "Error")
                Me.txtEventText.Focus()

                Exit Sub
            End If

            Dim Msg As New StringBuilder

            With Msg
                .Append(Me.txtEventText.Text)

                If Me.chkNotify.IsChecked Then
                    .Append(vbCrLf & vbCrLf)
                    .Append("*****************************************************************" & vbCrLf)
                    .Append(Now() & " : Edited by " & GlobalCRM.CurrentConName & " using admin tool")
                End If

                .Append("Event number " & Me.txtEventId.Text & " edited, details below")

                .Append(vbCrLf & vbCrLf)
                .Append("Old Type : " & Me._OldDescription & vbCrLf)
                .Append("New Type : " & Me.txtEventText.Text & vbCrLf)

                .Append(vbCrLf & vbCrLf)
                .Append("Old Message" & vbCrLf & Me._OldMessage & vbCrLf & vbCrLf)
                .Append("New Message" & vbCrLf & Me.txtEventText.Text)


                If (New TasksEvents).EditEvent(Me.txtEventId.Text, Me.txtEventType.Text.Trim, Me.txtEventText.Text.Trim, Msg.ToString) Then
                    Me.btnClose_Click(Me, Nothing)
                End If



            End With




        End If

    End Sub
End Class
